package com.zxm.sunshineaiary.view.calendar.datewidgetdaycell;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.animation.AlphaAnimation;
import android.widget.LinearLayout.LayoutParams;

/**
 * 日期按钮的类
 * 
 * @author Administrator
 * 
 */
public class DateWidgetDayCell extends View {

	public interface OnItemClick {
		public void OnDateClick(DateWidgetDayCell item);

		public void OnLongDateClick(DateWidgetDayCell item);
	}

	public int iDateYear = 0;
	public int iDateMonth = 0;
	public int iDateDay = 0;
	private static final int TOUCH_SLOP = 100;

	private final String TAG = "DateWidgetDayCell";
	private final int ANIM_ALPHA_DURATION = 100;
	private OnItemClick itemClick = null;
	private RectF rect = new RectF();
	private Paint pt = new Paint();
	private Runnable mLongPressRunnable = null;

	private boolean bTouchedDown = false;
	private boolean bSelected = false;

	private int iMargin = 0;
	private float fTextSize = 0;
	private boolean bToday = false;
	private boolean mDate = false;
	private boolean bIsActiveMonth = false;
	private boolean isMoved = true;
	private boolean isLongClick = false;

	private String sDate = "1";

	public DateWidgetDayCell(Context context, int iWidth, int iHeight) {
		super(context);
		setProperty();
		setFocusable(true);
		setLayoutParams(new LayoutParams(iWidth, iHeight));
		createThread();
	}

	/**
	 * 设置属性
	 */
	private void setProperty() {
		iMargin = DateWidgetDayInfo.getinstance().getiMargin();
		fTextSize = DateWidgetDayInfo.getinstance().getfTextSize();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Log.v(TAG, "onDraw");
		rect.set(0, 0, this.getWidth(), this.getHeight());
		rect.inset(1, 1);
		final boolean bFocused = IsViewFocused();
		drawDayView(canvas, bFocused); // 画背景
		drawDayNumber(canvas, bFocused); // 画数字
	}

	public boolean getSelected() {
		return bSelected;
	}

	@Override
	public void setSelected(boolean selected) {
		if (this.bSelected != selected) {
			this.bSelected = selected;
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		boolean bHandled = false;
		int x = (int) event.getX();
		int y = (int) event.getY();
		int mLastMotionX = 0;
		int mLastMotionY = 0;

		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			isLongClick = true;
			mLastMotionX = x;
			mLastMotionY = y;
			isMoved = false;
			postDelayed(mLongPressRunnable, ViewConfiguration
					.getLongPressTimeout());

			bHandled = true;
			bTouchedDown = true;
			invalidate();
			startAlphaAnimIn(DateWidgetDayCell.this);
			break;
		case MotionEvent.ACTION_MOVE:
			if (isMoved) {
				break;
			}
			if (Math.abs(mLastMotionX - x) > TOUCH_SLOP
					|| Math.abs(mLastMotionY - y) > TOUCH_SLOP) {
				// 移动超过阈值，则表示移动了
				isMoved = true;
				removeCallbacks(mLongPressRunnable);
			}
			break;
		case MotionEvent.ACTION_UP:// 释放了
			removeCallbacks(mLongPressRunnable);
			bHandled = true;
			bTouchedDown = false;
			invalidate();
			if (isLongClick) {
				doItemClick();
			}
			break;
		}
		return bHandled;
	}

	@Override
	protected void onFocusChanged(boolean gainFocus, int direction,
			Rect previouslyFocusedRect) {
		super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
	}

	public boolean IsViewFocused() {
		return (this.isFocused() || bTouchedDown);
	}

	/**
	 * 画日期中的方块背景
	 * 
	 * @param canvas
	 * @param bFocused
	 */
	private void drawDayView(Canvas canvas, boolean bFocused) {
		// if (bSelected || bFocused) {
		// LinearGradient lGradBkg = null;
		//
		//
		// if (bSelected) {
		// lGradBkg = new LinearGradient(rect.left, 0, rect.right, 0,
		// DayStyle.iColorBkgSelectedDark,
		// DayStyle.iColorBkgSelectedLight, Shader.TileMode.CLAMP);
		// }
		//
		// if (lGradBkg != null) {
		// pt.setShader(lGradBkg);
		// canvas.drawRect(rect, pt);
		// }
		//
		// pt.setShader(null);
		//
		// } else {

		if (bFocused) {
			pt.setColor(DayStyle.iColorBkgBlue);
		} else if (mDate) {
			pt.setColor(DayStyle.iColorBkgGreen);
		} else {
			pt.setColor(DayStyle.iColorBkgGray);
		}
		canvas.drawRect(rect, pt);
	}

	/**
	 * 执行单击事件
	 */
	public void doItemClick() {
		if (null != itemClick) {
			itemClick.OnDateClick(this);
		}
	}

	/**
	 * 设置按钮监听
	 * 
	 * @param itemClick
	 */
	public void setItemClick(OnItemClick itemClick) {
		this.itemClick = itemClick;
	}

	/**
	 * 画日期中的数字文字
	 * 
	 * @param canvas
	 * @param bFocused
	 */
	private void drawDayNumber(Canvas canvas, boolean bFocused) {

		// draw day number
		pt.setTypeface(null);
		pt.setAntiAlias(true);
		pt.setShader(null);
		pt.setFakeBoldText(true);
		pt.setTextSize(fTextSize);

		pt.setUnderlineText(false);
		if (bToday)
			pt.setUnderlineText(true);

		int iTextPosX = (int) rect.right - (int) pt.measureText(sDate);
		int iTextPosY = (int) rect.bottom + (int) (-pt.ascent())
				- getTextHeight();

		iTextPosX -= ((int) rect.width() >> 1)
				- ((int) pt.measureText(sDate) >> 1);
		iTextPosY -= ((int) rect.height() >> 1) - (getTextHeight() >> 1);

		int color = android.graphics.Color.WHITE;

		pt.setColor(color);
		if (bIsActiveMonth) {
			canvas.drawText(sDate, iTextPosX, iTextPosY + iMargin, pt);
		}

		pt.setUnderlineText(false);

	}

	/**
	 * 给日历设置数据
	 * 
	 * @param iYear
	 * @param iMonth
	 * @param iDay
	 * @param bToday
	 * @param bHoliday
	 * @param iActiveMonth
	 */
	public void setData(int iYear, int iMonth, int iDay, boolean bToday,
			boolean bHoliday, int iActiveMonth) {
		iDateYear = iYear;
		iDateMonth = iMonth;
		iDateDay = iDay;

		this.sDate = Integer.toString(iDateDay);
		this.bIsActiveMonth = (iDateMonth == iActiveMonth);
		this.bToday = bToday;
	}

	private int getTextHeight() {
		return (int) (-pt.ascent() + pt.descent());
	}

	/**
	 * 设置文字大小
	 * 
	 * @param size
	 */
	public void setTextSize(float size) {
		this.fTextSize = size;
	}

	/**
	 * 渐变动画
	 * 
	 * @param view
	 */
	public void startAlphaAnimIn(View view) {
		AlphaAnimation anim = new AlphaAnimation(0.5F, 1);
		anim.setDuration(ANIM_ALPHA_DURATION);
		anim.startNow();
		view.startAnimation(anim);
	}

	/**
	 * 注册监听事件
	 * 
	 * @param itemClick
	 */
	public void setOnItemClickListener(OnItemClick itemClick) {
		this.itemClick = itemClick;
	}

	/**
	 * 设置此日期是否存在日记，用于改变背景的判断
	 */
	public void setDateBackgroudTrue() {
		this.mDate = true;
	}

	public void setDateBackgroudFalse() {
		this.mDate = false;
	}

	private void createThread() {
		mLongPressRunnable = new Runnable() {
			@Override
			public void run() {
				isLongClick = false;
				itemClick.OnLongDateClick(DateWidgetDayCell.this);
			}
		};
	}
}
